/**
 * Project Name:template-web
 * File Name   :APIReturnHandler.java
 * Package Name:org.cn.template.component.view
 * Date:2015年9月27日下午4:22:51
 * Copyright (c) 2015, http://my.oschina.net/httpssl All Rights Reserved.
 *
*/

package org.cn.template.component.view;

import org.cn.template.component.domain.API;
import org.springframework.core.MethodParameter;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.method.support.ModelAndViewContainer;

import com.alibaba.fastjson.JSON;

/**
 * ClassName:APIReturnHandler <br/>
 * Function :TODO ADD FUNCTION. <br/>
 * Reason	:TODO ADD REASON. <br/>
 * Date     :2015年9月27日 下午4:22:51 <br/>
 * @author  :http://my.oschina.net/httpssl
 * @email   :491835898@QQ.COM
 * @since   :JDK 1.7
 * @see
 *  	 
 */
public class APIReturnHandler implements HandlerMethodReturnValueHandler
{
	
	/**
	 * 用来确定此实现类是否支持对应返回类型
	 * true 支持
	 * false 不支持
	 */
	@Override
	public boolean supportsReturnType(MethodParameter returnType) {
		Class<?> type = returnType.getParameterType();
		if(API.class.isAssignableFrom(type)){
			return true;
		}
		return false;
	}

	@Override
	public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer,
			NativeWebRequest webRequest) throws Exception {
		// TODO Auto-generated method stub
		if (returnValue == null) {
			mavContainer.setRequestHandled(true);
			return;
		}
		API api = (API) returnValue;
		mavContainer.addAttribute("api",JSON.toJSONString(api,true));
		mavContainer.setViewName("api");
	}
	
}
